<?php
namespace app\api\controller;

use app\common\model\CarServiceModel;
use think\Request;

class ServiceController extends BaseController{

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function index(Request $request){
        //成功返回数据
        //return backSuccess($data);
        //失败返回数据
        return backError("用户名错误！");
    }

    /**
     * 获取服务列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSvr(Request $request){
        $carSvr=new CarServiceModel();
        $list = $carSvr->getALL(['status'=>CarServiceModel::SHOW_STATUS]);

        return backSuccess($list);
    }

    /**
     * 服务详情
     * @param Request $request
     * @return array|null|\PDOStatement|string|\think\Model|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function details(Request $request){
        $id=$request->id;

        if(empty($id)){
            return backError("参数错误！");
        }

        $info=CarServiceModel::where('status',CarServiceModel::SHOW_STATUS)
            ->where("id",$id)
            ->find();

        if (empty($info)){
            return backError("参数错误！");
        }
//        $info['content'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
//            return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
//        }, $info['content']);

        return backSuccess($info);
    }

    //创建服务订单
    public function addOrder(Request $request){
        //创建订单
        $order_id = date('YmdHis') . mt_rand(100000, 999999);
        $data = $request->data;
        $user = $request->user;

        /**
        $data["service_id"] = $request->service_id;
        $data["real_name"] = $request->real_name;
        $data["mobile"] = $request->mobile;
        $data["brand_id"] = $request->brand_id;

        $data["address"] = $request->address;
        $data["go_time"] = $request->go_time;
        **/
        $data['user_id'] = $user['uid'];
        $data["order_number"] = $order_id;
        $data["create_time"] = time();
        $data["status"] = 0;
        $data["order_type"] = 0;
        $res = model("order")->insert($data);

        if($res == 1){
            return backSuccess([],"ok");
        } else {
            return backError("参数错误");
        }

    }

    //支付订单金额
    public function moneyPay(Request $request){

    }
}
